package org.jupiter.blog.web.controller;

import jakarta.validation.Valid;
import org.jupiter.blog.api.dto.JupiterUrlResourceDTO;
import org.jupiter.blog.api.dto.PageResponseDto;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterUrlResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * URL资源管理控制器
 */
@RestController
@RequestMapping("/url-resources")
public class JupiterUrlResourceController {

	@Autowired
	private JupiterUrlResourceService urlResourceService;

	/**
	 * 创建URL资源
	 */
	@PostMapping
	public JupiterResult<Void> createUrlResource(@RequestBody @Valid JupiterUrlResourceDTO dto) {
		urlResourceService.createUrlResource(dto);
		return JupiterResult.success(null);
	}

	/**
	 * 根据UUID获取URL资源
	 */
	@GetMapping("/{uuid}")
	public JupiterResult<JupiterUrlResourceDTO> getUrlResourceById(@PathVariable("uuid") String uuid) {
		return JupiterResult.success(urlResourceService.getById(uuid));
	}

	/**
	 * 更新URL资源
	 */
	@PutMapping("/{uuid}")
	public JupiterResult<Void> updateUrlResource(@PathVariable("uuid") String uuid,
			@RequestBody @Valid JupiterUrlResourceDTO dto) {
		urlResourceService.updateUrlResource(uuid, dto);
		return JupiterResult.success(null);
	}

	/**
	 * 删除URL资源
	 */
	@DeleteMapping("/{uuid}")
	public JupiterResult<Void> deleteUrlResource(@PathVariable("uuid") String uuid) {
		urlResourceService.deleteUrlResource(uuid);
		return JupiterResult.success(null);
	}

	/**
	 * 分页查询URL资源列表
	 */
	@PostMapping("/page")
	public JupiterResult<PageResponseDto<JupiterUrlResourceDTO>> getUrlResourcesByPage(
			@RequestBody JupiterUrlResourceDTO queryDTO) {
		List<JupiterUrlResourceDTO> urlResources = urlResourceService.getByPage(queryDTO);
		long total = urlResourceService.getCount(queryDTO);
		return JupiterResult.page(urlResources, total);
	}

	/**
	 * 获取所有URL资源
	 */
	@GetMapping
	public JupiterResult<List<JupiterUrlResourceDTO>> getAllUrlResources() {
		return JupiterResult.success(urlResourceService.getAll());
	}

}